package com.example.huwang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.huwang.R;
import com.example.huwang.model.LoginBean;
import com.example.huwang.utils.PermissionsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.huwang.app.MainApplication.PHONE;
import static com.example.huwang.app.MainApplication.dbController;

public class LoginActivity extends BaseActivity {
    List<LoginBean> mLoginUserList;

    @BindView(R.id.user_name)
    public TextView userName;
    @BindView(R.id.psw_word)
    public TextView pswWord;
    @BindView(R.id.login_btn)
    public Button loginBtn;
    @BindView(R.id.register_btn)
    public TextView registerBtn;
    @BindView(R.id.forget_btn)
    public TextView forgetBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //绑定初始化ButterKnife
        ButterKnife.bind(this);
        initData();
        initView();
        requestPermission();
    }

    private void initView() {
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (userName.getText().length() <= 0) {
                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_LONG).show();
                    return;
                }
                if (pswWord.getText().length() <= 0) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
                    return;
                }
                doLogin();
            }
        });

        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                intent.putExtra("type", "用手机号进行注册");
                startActivity(intent);
            }
        });
        forgetBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                intent.putExtra("type", "重置密码");
                startActivity(intent);
            }
        });
    }

    private void doLogin() {
        List<LoginBean> loginBeanList = dbController.searchByWhere(userName.getText().toString());
        if (loginBeanList.size() > 0) {
            for (LoginBean loginBean : loginBeanList) {
                if (loginBean.getPswWord().equals(pswWord.getText().toString())) {
                    PHONE = userName.getText().toString();
                    toMain();
                } else {
                    Toast.makeText(LoginActivity.this, "请输入正确的用户名或密码", Toast.LENGTH_LONG).show();
                }
            }
        } else {
            Toast.makeText(LoginActivity.this, "请输入正确的用户名或密码", Toast.LENGTH_LONG).show();
        }
    }


    private void requestPermission() {
        PermissionsUtils.getInstance().checkPermissions(LoginActivity.this, PermissionsUtils.PERMISSIONS_PHONE_SMS_NEED, new PermissionsUtils.IPermissionsResult() {
            @Override
            public void passPermissions() {
            }

            @Override
            public void forbidPermissions() {
                requestPermission();
            }
        });

    }

    private void toMain() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        this.finish();
    }

    private void initData() {
        dbController.deleteAll();
        insert();
        queryAllUser();
    }

    private void insert() {
        ArrayList<LoginBean> loginBeanArrayList = new ArrayList<>();
        LoginBean loginBean1 = new LoginBean();
        loginBean1.setUserName("13911112222");
        loginBean1.setPswWord("123456");
        LoginBean loginBean2 = new LoginBean();
        loginBean2.setUserName("13911113333");
        loginBean2.setPswWord("123456");
        loginBeanArrayList.add(loginBean1);
        loginBeanArrayList.add(loginBean2);
        // 插入新用户
        dbController.insert(loginBean1);
        dbController.insert(loginBean2);
    }

    private void queryAllUser() {
        mLoginUserList = dbController.searchAll();
        Log.e("****", "mLoginUserList.size():" + mLoginUserList.size());
    }

    private void update() {
        LoginBean loginBean = mLoginUserList.get(mLoginUserList.size() - 1);
        loginBean.setUserName("王二麻子");

//        DaoUtilsStore.getInstance().getUserDaoUtils().update(loginBean);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsUtils.getInstance().onRequestPermissionsResult(LoginActivity.this, requestCode, permissions, grantResults);
    }
}
